<?php


namespace app\admin\service\base;


use app\admin\model\base\Classes;
use app\admin\model\base\Grades;
use app\admin\model\base\Special;
use app\admin\model\base\Student;
use app\admin\validates\QuestionValidate;
use app\admin\validates\SpecialValidate;
use app\BaseService;
use think\exception\ValidateException;
use think\response\Json;

class SpecialService extends BaseService
{
    /**
     * 班级列表
     * @param array $params
     * @return Json
     */
    public function index(array $params): Json
    {
        $where = ['is_del'=>0];
        if(isset($params['name']) && $params['name']){
            $where['name'] = $params['name'];
        }
        //获取列表
        $data = Special::getList($where, $params['page'] ?? 1, $params['limit'] ?? 100, 'id desc');
        return jsonReturn(1, $data);
    }

    /**
     * 班级列表
     * @param array $params
     * @return Json
     */
    public function lists(array $params): Json
    {
        $data = Special::lists();
        return jsonReturn(1, $data);
    }

    /**
     * 详情
     * @param array $params
     * @return Json
     */
    public function read(array $params): Json
    {
        //数据验证
        try {
            validate(SpecialValidate::class)->scene('details')->check($params);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return jsonReturn(0, [], $e->getError());
        }
        $where = ['id'=>$params['id']];
        $data = Special::findOne($where);
        return jsonReturn(1, $data);
    }

    /**
     * 保存班级
     * @param array $params
     * @return Json
     */
    public function save(array $params): Json
    {
        //数据验证
        try {
            validate(SpecialValidate::class)->scene('add')->check($params);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return jsonReturn(0, [], $e->getError());
        }
        $data = Special::create(['name'=>$params['name'],'sort'=>$params['sort'] ?? 1,'level'=>$params['level'] ?? 1 ,'pid'=>$params['pid'] ?? 0]);
        if($data){
            return jsonReturn(1, []);
        }
        return jsonReturn(0, []);
    }

    /**
     * 编辑班级
     * @param array $params
     * @return Json
     */
    public function update(array $params): Json
    {
        //数据验证
        try {
            validate(SpecialValidate::class)->scene('edit')->check($params);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return jsonReturn(0, [], $e->getError());
        }
        $data = Special::where('id','=',$params['id'])
            ->update(['name'=>$params['name'],'sort'=>$params['sort'] ?? 1,'level'=>$params['level'] ?? 1 ,'pid'=>$params['pid'] ?? 0]);
        if($data){
            return jsonReturn(1, []);
        }
        return jsonReturn(0, []);
    }

    /**
     * 编辑
     * @param array $params
     * @return Json
     */
    public function delete(array $params): Json
    {
        //数据验证
        try {
            validate(SpecialValidate::class)->scene('del')->check($params);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return jsonReturn(0, [], $e->getError());
        }
        if(Grades::where('special_id','=',$params['id'])->count() > 0){
            return jsonReturn(0, [],'该专业下面还有年级，请先删除关联的年级');
        }
        if(Student::where('special_id','=',$params['id'])->count() > 0){
            return jsonReturn(0, [],'该年级下面还有学生，请先删除关联的学生');
        }
        $data = Special::where('id','=',$params['id'])->update(['is_del'=>1]);
        if($data){
            return jsonReturn(1, []);
        }
        return jsonReturn(0, []);
    }
}